//
//  SinaChooseView.swift
//  WeiBo01
//
//  Created by mac on 16/1/10.
//  Copyright © 2016年 Han. All rights reserved.
//

import UIKit

let SinaChooseViewReuseIdentifier = "SinaChooseViewReuseIdentifier"
//cell代理回调协议
protocol SinaChooseViewCellDelegate: NSObjectProtocol {
    
    //方法
    func didDeletePhoto(index: NSIndexPath)
}

class SinaChooseView: UICollectionView,UICollectionViewDataSource,SinaChooseViewCellDelegate {

    /// 创建一个私有数组来盛放图片
    private lazy var images: [UIImage] = [UIImage]()
    
    //公开一个方法,来添加图片
    func addimage(image: UIImage) {
        
        images.append(image)
        
        //刷新
        reloadData()
    }
    
    
    //1.
    init(){
        
        super.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.
    private func setupUI(){
        
        //注册cell
        registerClass(SinaChooseViewCell.self, forCellWithReuseIdentifier: SinaChooseViewReuseIdentifier)
        
        //设置代理
        dataSource = self
        
        //设置itemSize
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        let chooseViewItemMargin: CGFloat = 5
        let itemWH = (kUIScreenWidth - 2*SinaHomeMargin - 2*chooseViewItemMargin) / 3
        
        flowLayout.itemSize = CGSizeMake(itemWH, itemWH)
        
        //设置间距
        flowLayout.minimumInteritemSpacing = chooseViewItemMargin
        flowLayout.minimumLineSpacing = chooseViewItemMargin
      
        
    }
    //实现代理方法
    func didDeletePhoto(index: NSIndexPath) {

        printLog("删除图片")
        images.removeAtIndex(index.item)
        //刷新
        reloadData()
    }
    
}

extension SinaChooseView{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        // 当有0和9张图片的时候,不显示加号按钮
        // 其他的时候,显示加号按钮 , images.count + 1

        if images.count == 0 || images.count == 9 {
            
            return images.count
        }else{
//            printLog("返回count\(images.count + 1)")

            return images.count + 1
            
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SinaChooseViewReuseIdentifier, forIndexPath: indexPath) as! SinaChooseViewCell
        
        //改变测试颜色
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.yellowColor() : UIColor.greenColor()
        
        //设置代理
        cell.delegate = self
        
        if indexPath.item == images.count {
            //设置cell显示加号
            cell.image = nil
            
//            printLog("加号\(indexPath.item)")
//            printLog("加号\(images.count)")
        }else{
            
            //赋值
            cell.image = images[indexPath.item]
//            printLog("============")
//            printLog(indexPath.item)
//            printLog(images.count)
        }
        //索引赋值
        cell.index = indexPath
        
        return cell
        
    }
    
}

class SinaChooseViewCell: UICollectionViewCell {
    
    /// 定义一个索引,用于删除哪个图片
    var index: NSIndexPath?
    
    //定义一个 delegate
    weak var delegate: SinaChooseViewCellDelegate?
    
    
    //定义模型
    var image: UIImage?{
        
        didSet{
            
            //如果图片为nil,显示加号
            if image == nil {
                
                iconImageView.image = UIImage(named: "compose_pic_add")
                deleteButton.hidden = true
            }else{
                iconImageView.image = image
                deleteButton.hidden = false
            }
            
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addSubview(iconImageView)
        addSubview(deleteButton)
        
        //就一个图片的时候
        iconImageView.frame = bounds
        
        deleteButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
        }
        
    }
    
    //MARK:--懒加载
    lazy var iconImageView: UIImageView = UIImageView()
    /// 删除按钮
    lazy var deleteButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        //大小
        button.sizeToFit()
        
        button.addTarget(self, action: "delete", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    func delete() {
        
        delegate?.didDeletePhoto(index!)
    }
    
}








